﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using ProtoImaginer.ProtoPage;

namespace ProtoImaginer.ProtoControl
{
    public partial class ControlAtom : UserControl
    {
        private Models.Control entity;
        private PageView parent;

        /*====================================================================
         * Binding UI to data
         *==================================================================*/
        public ControlAtom()
        {
            //entity = new Control();
            InitializeComponent();
            this.parent = null;
        }
        public ControlAtom(PageView parent)
        {
            //entity = new Control();
            InitializeComponent();
            this.parent = parent;
        }
        public ControlAtom(Models.Control controlDB)
        {
            InitializeComponent();
            entity = controlDB;
        }
        /*====================================================================
         * Events handler
         *==================================================================*/
        private void ControlDotDragStarted(object sender, DragStartedGestureEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;
            }
        }

        private void ControlDotDragCompleted(object sender, DragCompletedGestureEventArgs e) {

            if (!e.Handled)
            {
                e.Handled = true;

                this.CheckMinSize();
                parent.CheckRangeWithinCanvas(this);
            }
        }

        private void ControlDotADrag(object sender, DragDeltaGestureEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;

                if ((this.Width - e.HorizontalChange >= this.MinWidth) && (this.Height - e.VerticalChange >= this.MinHeight))
                {
                    //resize 
                    this.Width = Math.Abs(this.Width - e.HorizontalChange);
                    this.Height = Math.Abs(this.Height - e.VerticalChange);

                    //keep control item still 
                    Canvas.SetLeft(this, Canvas.GetLeft(this) + e.HorizontalChange);
                    Canvas.SetTop(this, Canvas.GetTop(this) + e.VerticalChange);
                }
            }
        }

        private void ControlDotBDrag(object sender, DragDeltaGestureEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;

                if ((this.Width + e.HorizontalChange >= this.MinWidth) && (this.Height - e.VerticalChange >= this.MinHeight))
                {
                    //resize 
                    this.Width = Math.Abs(this.Width + e.HorizontalChange);
                    this.Height = Math.Abs(this.Height - e.VerticalChange);

                    //keep control item still 
                    //Canvas.SetLeft(this, Canvas.GetLeft(this) + e.HorizontalChange);
                    Canvas.SetTop(this, Canvas.GetTop(this) + e.VerticalChange);
                }
            }
        }

        private void ControlDotCDrag(object sender, DragDeltaGestureEventArgs e)
        {

            if (!e.Handled)
            {
                e.Handled = true;

                if ((this.Width + e.HorizontalChange >= this.MinWidth) && (this.Height + e.VerticalChange >= this.MinHeight))
                {
                    //resize 
                    this.Width = Math.Abs(this.Width + e.HorizontalChange);
                    this.Height = Math.Abs(this.Height + e.VerticalChange);

                    //keep control item still 
                    //Canvas.SetLeft(this, Canvas.GetLeft(this) - e.HorizontalChange);
                    //Canvas.SetTop(this, Canvas.GetTop(this) - e.VerticalChange);
                }
            }
        }

        private void ControlDotDDrag(object sender, DragDeltaGestureEventArgs e)
        {
            if (!e.Handled)
            {
                e.Handled = true;

                if ((this.Width - e.HorizontalChange >= this.MinWidth) && (this.Height + e.VerticalChange >= this.MinHeight))
                {
                    //resize 
                    this.Width = Math.Abs(this.Width - e.HorizontalChange);
                    this.Height = Math.Abs(this.Height + e.VerticalChange);

                    //keep control item still 
                    Canvas.SetLeft(this, Canvas.GetLeft(this) + e.HorizontalChange);
                    //Canvas.SetTop(this, Canvas.GetTop(this) + e.VerticalChange);
                }
            }
        }

        /*====================================================================
         * non Event-handler UI changes
         *==================================================================*/

        private void CheckMinSize()
        {
            if (this.Width < this.MinWidth)
                this.Width = this.MinWidth;

            if (this.Height < this.MinHeight)
                this.Height = this.MinHeight;
        }

        public void Outline(bool flag)
        {
            if (flag)
            {
                this.ControlRect.StrokeThickness = 5;
                this.ControlRect.Opacity = 0.8;
                this.ControlDotA.Visibility = Visibility.Visible;
                this.ControlDotB.Visibility = Visibility.Visible;
                this.ControlDotC.Visibility = Visibility.Visible;
                this.ControlDotD.Visibility = Visibility.Visible;
            }
            else
            {
                this.ControlRect.StrokeThickness = 0;
                this.ControlRect.Opacity = 0.3;
                this.ControlDotA.Visibility = Visibility.Collapsed;
                this.ControlDotB.Visibility = Visibility.Collapsed;
                this.ControlDotC.Visibility = Visibility.Collapsed;
                this.ControlDotD.Visibility = Visibility.Collapsed;
            }
        }
    }
}